<template>
  <div>
    <!-- 表格 -->
    <el-table style="width: 100%" border :data="records">
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column
      ><el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column
      ><el-table-column label="默认图片" width="120">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" style="width: 100px" />
        </template> </el-table-column
      ><el-table-column prop="weight" label="重量(kg)" width="80">
      </el-table-column
      ><el-table-column prop="price" label="价格(元)" width="80">
      </el-table-column
      ><el-table-column label="操作" width="300">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            icon="el-icon-sort-down"
            size="mini"
            v-if="row.isSale == 0"
            @click="sale(row)"
          ></el-button>
          <el-button
            type="success"
            icon="el-icon-sort-up"
            size="mini"
            v-else
            @click="cancel(row)"
          ></el-button
          ><el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit"
          ></el-button
          ><el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-popconfirm
            :title="`确定删除${row.skuName}吗？`"
            style="margin-left: 10px"
            @onConfirm="deleteSku(row)"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              slot="reference"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      :current-page="page"
      layout=" prev, pager, next, jumper,->,sizes, total"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      :total="total"
      style="text-align: center"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    >
    </el-pagination>
    <!-- 抽屉效果 -->
    <el-drawer :visible.sync="show" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag
              type="success"
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              style="margin=right: 10px"
              >{{ item.attrId }}-{{ item.valueId }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品照片</el-col>
        <el-col :span="16">
          <el-carousel height="300">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" style="width: 100%" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1, //当前的页数
      limit: 10, //每页显示的数据
      records: [], //存储获取到的列表数据
      total: 0, //存储分页器一共有多少数据
      skuInfo: {}, //存储详情的数据
      show: false, //抽屉的显示与隐藏
    };
  },
  // 组件挂载完毕
  mounted() {
    // 获取sku的方法
    this.getSkuList();
  },
  methods: {
    // 发请求 获取sku的数据
    async getSkuList(pages = 1) {
      this.page = pages;
      // 结构出需要的参数
      const { page, limit } = this;
      let result = await this.$API.sku.reqSkuList(page, limit);
      // console.log(result);
      if (result.code) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    // 修改每页展示的数据
    handleSizeChange(limit) {
      // 修改参数
      this.limit = limit;
      // 刷新列表
      this.getSkuList();
    },
    // 上架的回调
    async sale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      // console.log(result);
      if (result.code == 200) {
        // 将isSale改为1代表上架
        row.isSale = 1;
        this.$message.success("上架成功");
      }
    },
    // 下架的回调
    async cancel(row) {
      let result1 = await this.$API.sku.reqCanael(row.id);
      // console.log(result1);
      if (result1.code == 200) {
        // 将isSale改为0代表下架
        row.isSale = 0;
        this.$message.success("下架成功");
      }
    },
    // 修改按钮的回调
    edit() {
      this.$message("正在开发中");
    },
    // 获取SKU详情的方法
    async getSkuInfo(sku) {
      // 发请求获取数据
      let result = await this.$API.sku.reqSkuById(sku.id);
      // console.log(result);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
      // 显示抽屉
      this.show = true;
    },
    // 删除SPU的回调
    async deleteSku(row) {
      let result = await this.$API.sku.reqDeleteSku(row.id);
      if (result.code == 200) {
        this.$message.success("删除成功！");
        // records是当前页面数据的个数 如果删除后 数据个数大于1就留在当前页面 小于1就返回到上一页
        this.getSkuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
  },
};
</script>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
/* 深度选择器:>>>用于原生css /deep/用于less ::v-deep用于scss */
>>> .el-carousel__button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
</style>